﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Timers;
using LimitOrderBookSim.Assets;
using LimitOrderBookSim.Commons;
using LimitOrderBookSim.Model.Mock;
using LimitOrderBookSim.Model.Markets;

namespace LimitOrderBookSim
{

    //public  class Writer
    //{
    //    private Object writerLock = new Object();
    //    private StreamWriter writer;

    //    public Writer(String file)
    //    { #if LOG
    //        writer = new StreamWriter(file,true);
    //        #endif
    //    }

    //    public  void Write(String text)
    //    {
    //        #if LOG
    //            Monitor.Enter(writer);
    //            writer.Write(text);
    //            Monitor.Exit(writer);
    //        #endif
    //    }
    //}

    class Program
    {

        static void Main(string[] args)
        {

            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new ElapsedEventHandler(PrintLog);
            timer.Interval = 10000;
            
            for (int i = 0; i < 10; i++)
            {
                new Player(Builder.Markets).Play();
            }

               // random = new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds);
                //System.Timers.Timer timer = new System.Timers.Timer();
                //timer.Elapsed += new ElapsedEventHandler(PrintLog);
                //timer.Interval = 10000;

                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);
                //ThreadPool.QueueUserWorkItem(Randomizer, null);

                timer.Start();

              Console.Read();
        }


        public static void PrintLog(Object obj, EventArgs e)
        {
            lock (Builder.Markets)
            {
                foreach (Market m in Builder.Markets)
                {
                    Console.Out.WriteLine("MARKET :" + m.Name);

                    foreach (Event ev in m.Events)
                    {
                        Console.Out.WriteLine("     EVENT :" + ev.Name);

                        foreach (Contract c in ev.Contract)
                        {
                             Console.Out.WriteLine("           CONTRACT : " + c.Name);
                             Console.Out.WriteLine("           Bid/Ask : " +  c.Pool.BOOK.BestBidAsk());
                             Console.Out.WriteLine("           Avg Bid/Ask : " + c.Pool.BOOK.AvgBidAsk());
                             Console.Out.WriteLine("           High : " +  c.Pool.BOOK.High());
                             Console.Out.WriteLine("           Low : " +  c.Pool.BOOK.Low());
                             Console.Out.WriteLine("           Volume : " +  c.Pool.BOOK.Volume);
                             Console.Out.WriteLine();
                        }
                    }
                }
            }
        }


        public static void Randomizer(Object obj)
        {
            while (true)
            {
                //OrderPool.Add(RandomOrder());
            }
        }
        
        //public static Message RandomOrder()
        //{
        //  Message msg  = new Message()
        //     {
        //         Event = EVENT.SUBMISSION,
        //         Timestamp = DateTime.Now.Ticks,
        //         Order = new Order()
        //              {
        //                  ID = -1,
        //                  Price = Math.Round((random.NextDouble()+0.1) * 10, 2),
        //                  Size = random.Next(1, 15),
        //                  //Symbol = "AA",
        //                  Type = (random.Next(0,2)==1)?TYPE.ASK: TYPE.BID
        //              }
        //     };

        //    return msg;
        //}
    }
}
